import logging
import os
import signal
import time
import sys

# utf-8编码
logging.basicConfig(filename='error/info.log', level=logging.INFO, encoding='utf-8')

def signal_handler(s, frame):
    # 记录日志
    logging.info('接收到信号: %s', s)
    print('Signal handler called with signal', s)
    # 释放example.log文件句柄
    logging.shutdown()
    sys.stdout.flush()
    sys.stderr.flush()
    os._exit(0)

signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGSEGV, signal_handler)

try:
    while True:
        time.sleep(1)  # import time
        # 抛出异常
        raise Exception('抛出异常')
except Exception as e:
    logging.error('程序异常退出: %s', e)
    raise